'use strict';

const Controller = require('egg').Controller;
const uuid = require('uuid');
class CurdController extends Controller {
  async add () {
    // 获取前端传来的参数
    const { ctx } = this;
    ctx.body = ctx.request.body;
    // 处理:比如传给service去查询数据库
    const users = await this.ctx.service.curdService.add(ctx.body);
    console.log(users);
    // 返回
    ctx.body = users;
  }

  /**
   * 申请权限接口
   */
  async addApplyUser () {
    // 获取前端传来的参数
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};
    let transaction;
    try {
      // 建立事务对象
      transaction = await this.ctx.model.transaction();
      // 处理:比如传给service去查询数据库
      const users = await this.ctx.service.curdService.addApplyUser(ctx.body, transaction);
      //  const permission = await this.ctx.service.permissionsService.add(ctx.body, transaction);
      //  console.log(permission);
      // 提交事务
      await transaction.commit();
      retrunMap = { code: '1', msg: '成功' };
    } catch (err) {
      // 事务回滚
      await transaction.rollback();
      retrunMap = { code: '0', msg: '失败' };
    }

    // 返回
    ctx.body = retrunMap;
  }

  /**
   * 保存客户端的权限申请
   */
  async save () {
    const { ctx } = this;
    // 1.获取前端传来的请求授权信息
    ctx.body = ctx.request.body;
    console.log(ctx.body);
    let retrunMap = {};
    let transaction;
    const region = ctx.body.region;
    region.id = uuid.v1().replace(new RegExp('-', 'gm'), '');
    try {
      delete ctx.body.region;
      ctx.body.region_id = region.id;

      // 建立事务对象
      transaction = await this.ctx.model.transaction();
      // 处理:比如传给service去查询数据库
      await this.ctx.service.curdService.addApplyUser(ctx.body, transaction);
      const regions = await this.ctx.service.regionService.add(region, transaction);
      console.log(regions);
      // 提交事务
      await transaction.commit();
      retrunMap = { code: '1', msg: '成功' };
    } catch (err) {
      // 事务回滚
      await transaction.rollback();
      console.log(err);
      retrunMap = { code: '0', msg: '失败' };
    }

    // 返回
    ctx.body = retrunMap;

  }

  /**
   * 根据id获取数据
   */
  async getDataByPk () {
    const { ctx } = this;
    const id = ctx.params.id;
    const users = await this.ctx.service.curdService.getDataByPk(id);

    ctx.body = users;
  }

  /**
   * 更新用户数据
   */

  async updateUser () {
    const { ctx } = this;
    ctx.body = ctx.request.body;// post json
    console.log(ctx.body);
    // const id = ctx.params.id;
    const users = await this.ctx.service.userService.updateUser(ctx.body);
    if (users) {
      ctx.body = { code: '1', msg: '更改成功' };
    } else {
      ctx.body = { code: '0', msg: '更改失败' };
    }
  }

  /**
   * 删除用户数据
   */
  async deleteUser () {
    const { ctx } = this;
    const id = ctx.params.id;
    const users = await this.ctx.service.curdService.deleteUser(id);

    ctx.body = users;
  }

}

module.exports = CurdController;
